#include "GGE.h"

void GGE_TEXTURE::Load( const char *varGGE_PATH_IMG )
{
  varGGE_SURFACE = IMG_Load ( varGGE_PATH_IMG );
}

void GGE_TEXTURE::Uload()
{
  SDL_FreeSurface ( varGGE_SURFACE );
}

GGE_Entity2D::GGE_Entity2D( int nf )
{
  varGGE_SPRITE = new GGE_TEXTURE[nf];
  varGGE_NFRAMES = nf;
  varGGE_CONT = 0;

}

GGE_Entity2D::GGE_Entity2D()
{
  int nf=1;
  varGGE_SPRITE = new GGE_TEXTURE[nf];
  varGGE_NFRAMES = nf;
  varGGE_CONT = 0;
  varGGE_SCALE = 1;
  varGGE_COLOR_R = 255;
  varGGE_COLOR_G = 255;
  varGGE_COLOR_B = 255;
  varGGE_ALPHA   = 1;
}

void GGE_Entity2D::Addframe( GGE_TEXTURE frameImg )
{
  if (varGGE_CONT<varGGE_NFRAMES)
    {
      varGGE_SPRITE[varGGE_CONT]= frameImg;
      varGGE_CONT++;
    }
}

void GGE_Entity2D::Draw ( int _x, int _y  )
{


  varGGE_NCOLORS = varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->format->BytesPerPixel;
  if (varGGE_NCOLORS == 4)
    {
      if (varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->format->Rmask == 0x000000ff)
        varGGE_TEXTURE_FORMAT = GL_RGBA;
      else
        varGGE_TEXTURE_FORMAT = GL_BGRA;
    }
  else if (varGGE_NCOLORS == 3)
    {
      if (varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->format->Rmask == 0x000000ff)
        varGGE_TEXTURE_FORMAT = GL_RGB;
      else
        varGGE_TEXTURE_FORMAT = GL_BGR;
    }
  else
    {
      printf("Atencion: La imagen no es de color real, puede que cause errores.\n");
    }
  glGenTextures( 1, &varGGE_TEXTURE );
  glBindTexture( GL_TEXTURE_2D, varGGE_TEXTURE );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  glTexImage2D( GL_TEXTURE_2D, 0, varGGE_NCOLORS, varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->w, varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->h, 0,
                varGGE_TEXTURE_FORMAT, GL_UNSIGNED_BYTE, varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->pixels );
  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  glBindTexture( GL_TEXTURE_2D,varGGE_TEXTURE );

  glLoadIdentity();
  glPushMatrix();
  glColor4f( varGGE_COLOR_R , varGGE_COLOR_G, varGGE_COLOR_B, varGGE_ALPHA );
  glTranslatef ( _x  , _y  ,0 );
  glRotatef( varGGE_ROTATION  ,0.0f,0.0f,1.0);
  glScalef ( varGGE_SCALE, varGGE_SCALE, 0 );
  glTranslatef ( - varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->w/2 , - varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->h/2 ,0.0f );

  glBegin( GL_QUADS );
  glTexCoord2i( 0, 0 );
  glVertex3f( varGGE_X, varGGE_Y, 0 );
  glTexCoord2i( 1, 0 );
  glVertex3f( varGGE_X + varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->w, varGGE_Y, 0 );
  glTexCoord2i( 1, 1 );
  glVertex3f( varGGE_X + varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->w, varGGE_Y + varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->h, 0 );
  glTexCoord2i( 0, 1 );
  glVertex3f( varGGE_X, varGGE_Y + varGGE_SPRITE[varGGE_STATE].varGGE_SURFACE->h, 0 );
  glEnd();
  glFinish();
  glDeleteTextures(1, &varGGE_TEXTURE);
}

void GGE_Entity2D::Rotate ( float rotation )
{
  varGGE_ROTATION = rotation;
}

void GGE_Entity2D::Scale ( float scale )
{
  varGGE_SCALE = scale;
}

void GGE_Entity2D::Color ( float _r, float _g, float _b )
{
  varGGE_COLOR_R = _r;
  varGGE_COLOR_G = _g;
  varGGE_COLOR_B = _b;
}

void GGE_Entity2D::Alpha ( float _alpha )
{
  varGGE_ALPHA = _alpha;
}
